$(document).ready(function(){
	var isLoggedIn = 0;
	$.ajax({
		type: "POST",
		url: "/audivit/users/checklogin",
		success: function(msg){
			isLoggedIn = msg;
		}
	});
	$(".arrow_up").bind("click",{direct: '1'},clickArrow);
	$(".arrow_down").bind("click",{direct: '0'},clickArrow);

	function clickArrow(event) {
		songId = $(this).attr('id').match(/\d+/);
		$("#down_arrow_"+songId).attr('onclick','').unbind('click');
		$("#up_arrow_"+songId).attr('onclick','').unbind('click');
		direct = event.data.direct;
		if(isLoggedIn == 1){
			$.ajax({
				type: "POST",
				data: {song_id: songId, direction: direct},
				url: "/audivit/scores/addAjax",
				async: false,
				success: function(msg){
					$("#score_"+songId).text(msg);
					if (direct=='1'){
						$("#down_arrow_"+songId).bind("click",{direct: '0'},clickArrow);
						$("#down_arrow_"+songId).css("background-image","url(http://"+window.location.host+"/audivit/css/images/arrow_down.png)");
						$("#up_arrow_"+songId).css("background-image","url(http://"+window.location.host+"/audivit/css/images/arrow_up_glowing.png)");
					} else if (direct=='0'){
						$("#up_arrow_"+songId).bind("click",{direct: '1'},clickArrow);
						$("#down_arrow_"+songId).css("background-image","url(http://"+window.location.host+"/audivit/css/images/arrow_down_glowing.png)");
						$("#up_arrow_"+songId).css("background-image","url(http://"+window.location.host+"/audivit/css/images/arrow_up.png)");
					}
				}
			});
		}else{
			window.location.replace("/audivit/users/login");
		}
	}	

	//sliding down and up
	$(".song_title").bind("click", clickTitle);

	function clickTitle(event) {
		titleId = $(this).attr('id');
		playId = "#song_play_"+titleId.match(/\d+/);
		$(playId).slideDown();
		$(this).unbind("click");
		$(this).click(function(){
			titleId = $(this).attr('id');
			playId = "#song_play_"+titleId.match(/\d+/);
			$(playId).slideUp();
			$(this).unbind("click");
			$(this).bind("click", clickTitle);
		});
	}
});

